March 97 - Kon & Bal's Puzzle Page
KON & BAL'S PUZZLE PAGE
AppendDITL Apoplexy
Martin-Gilles Lavoie and Bo3b Johnson
See if you can solve this puzzle in the form of a dialog between a
pseudo KON (Bo3b Johnson) and BAL (Martin-Gilles Lavoie). The
dialog gives clues to help you. Keep guessing until you're done; your
score is the number to the left of the clue that gave you the
correct answer. Even if you never run into the particular problems
being solved here, you'll learn some valuable debugging techniques
that will help you solve your own programming conundrums. And
you'll also learn interesting Macintosh trivia.
BAL Well, KON, I'm very disappointed to announce that I've fallen back to the level of a
newbie Mac programmer and am forced to ask you a question whose answer is probably
obvious. However, I'm stuck.
KON An easy one? Great, we'll have it fixed in no time -- and we'll give Puzzle Page
readers a chance to get a decent score for a change. Details, please.
BAL I'm working on a new version of our application plug-in. One of the dialogs we had
in the previous versions was very cluttered, and since we needed to add even more
stuff to it, we decided to use "tabs" to group related options. But now the system
crashes shortly after calling AppendDITL.
KON That's a well-traveled piece of the system. Why would you crash and no one else?
Let's check for reported bugs...just as I thought, Developer Technical Support has no
bugs listed for AppendDITL. Is the bug reproducible?
BAL 100% reproducible, and always after calling AppendDITL.
KON When is this AppendDITL call made?
BAL The plug-in code runs as part of the host application and monitors activity within
a floating window. One button in this floating window brings up a dialog containing a
list of items that can be edited with a second dialog that comes up. The second dialog has
tabs; when it's brought up, a call to AppendDITL is made to add the dialog items for the
first tab panel into this dialog.
KON How does it crash, exactly?
100 BAL A little while after AppendDITL exits, the dialog is visually messed up, and
the system drops into MacsBug with a bus error. Also, the application heap is usually
corrupted.
KON This is a code resource, right? Code resources can't have A5 globals, so any
globals would cause you to "color outside the lines." Are you using globals?
BAL The application plug-in interface file requires the use of globals, but it was built
with CodeWarrior, which uses register A4 as a code resource's globals pointer.
KON What if AppendDITL trashes A4 during execution? That would cause some of your
user item procedures to fail while trying to access globals.
90 BAL Well, let's check. When the dialog is first brought up and items are being
appended to it for the default tab, it immediately crashes. Here, witness the disaster...
KON Indeed, it crashes really hard. MacsBug is alive, but the system is barely alive; I
can't escape the application with es. Whoa! I can't even reboot withrb. Let's try that
again, and watch AppendDITL. We do an atb AppendDITL, then trace over the call.
Nope, A4 is untouched by AppendDITL. In addition, the heap was corrupted only after
several AppendDITL calls, so I'd say A4 is solid and not the problem.
BAL Time for the Vulcan Nerve Pinch?
KON Control-Command-Power? Indeed! While we're rebooting, can I see the code
where you add items to this dialog?
80 BAL My plug-in uses code that I've used in a standalone application. This code has
worked just fine since then, and I don't see why it should make a difference when used
in a code resource. It's pretty much by the book. When the user clicks the tabs in the
dialog, we make a DITL content switch using this code:
// At this point, clickedTab contains a DITL ID corresponding to
// the clicked tab DITL ID. 'kPanelBase' is the number of items